package org.ariadne_eu.hcifetcher.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ariadne_eu.hcifetcher.util.DateManager;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class AddAnswerServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(AddAnswerServlet.class.getName());
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String student = req.getParameter("studentid");
		String questionid = req.getParameter("questionid");
		String answerBody = req.getParameter("answer");
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Entity answer = new Entity("Answer");
		answer.setProperty("studentid", student);
		answer.setProperty("questionid", questionid);
		answer.setProperty("answer", answerBody);
		answer.setProperty("score",0);
		answer.setProperty("datestamp", new Date());
		Key k = datastore.put(answer);
		resp.getWriter().write("Added answer by " + student + " on question with ID: " + questionid + ". Answer body: \"" + answerBody + "\". Key: " + k);
		log.log(Level.INFO, "Added answer by " + student + " on question with ID: " + questionid + ". Answer body: \"" + answerBody + "\". Key: " + k);
	}
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req,resp);
	}


}
